package com.sunnada.edu.business.project.controller;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;

import com.alibaba.fastjson.JSONObject;
import com.sunnada.edu.business.project.service.ProjectMethods;
import com.sunnada.edu.business.project.service.ProjectService;
import com.sunnada.edu.system.pub.common.BasicController;
import com.sunnada.edu.system.pub.dao.Page;
import com.sunnada.edu.system.pub.service.PubMethods;
import com.sunnada.edu.system.pub.util.Utils;

/***
 * explain 工程控制层
 * 
 * @author chs 2015-5-12 version 1.0
 */
@Controller
@RequestMapping("/projectController")
public class ProjectController extends BasicController {
	@Resource
	ProjectService projectService;

	/** 工程首页 */
	@RequestMapping("/project")
	public String project(HttpServletRequest request) {
		String curr = request.getParameter("curr");
		request.setAttribute("curr", curr);
		// 当前路径
		String moduleId = request.getParameter("moduleId");
		request.setAttribute("pathValue", PubMethods.getPathValue(moduleId));
		return "business/project/project";
	}

	/** 工程查询 */
	@RequestMapping("/projectSearch")
	public void projectSearch(HttpServletRequest request,
			HttpServletResponse response) {
		HashMap<String, String> params = new HashMap<String, String>();
		setReqParams(request, null, params);
		Page page = new Page();
		page.setRowCount(projectService.getProjectCots(params));
		page.setPageMap(request, page, params);
		List<?> projectList = projectService.findProjects(params);
		JSONObject json = new JSONObject();
		json.put("list", projectList);
		json.put("params", params);
		printInfo(response, json);
	}

	/** 工程添加页面 */
	@RequestMapping("/projectAdd")
	public String projectAdd(HttpServletRequest request) {
		HashMap<String, String> params = new HashMap<String, String>();
		return "business/project/project_add";
	}

	/** 工程修改页面 */
	@RequestMapping("/projectUpdate")
	public String projectUpdate(HttpServletRequest request) {
		HashMap<String, String> params = new HashMap<String, String>();
		setReqParams(request, null, params);
		Map project = projectService.getProjectByRecId(params);
		request.setAttribute("project", project);
		return "business/project/project_update";
	}

	/** 工程详情页面 */
	@RequestMapping("/projectDetail")
	public String projectDetail(HttpServletRequest request) {
		HashMap<String, String> params = new HashMap<String, String>();
		setReqParams(request, null, params);
		Map project = projectService.getProjectByRecId(params);
		request.setAttribute("project", project);
		return "business/project/project_detail";
	}

	/** 工程保存功能 */
	@RequestMapping("/projectSave")
	public void projectSave(HttpServletRequest request,
			HttpServletResponse response) {
		HashMap<String, String> params = new HashMap<String, String>();
		setReqParams(request, null, params);
		String recId = params.get("recId");
		if (Utils.isNotNull(recId)) {
			msg = projectService.projectUpdate(params,request);
		} else {
			msg = projectService.projectAdd(params,request);
		}
		printInfo(response, msg);
	}

	/** 工程删除功能 */
	@RequestMapping("/projectDelete")
	public void projectDelete(HttpServletRequest request,
			HttpServletResponse response) {
		HashMap<String, String> params = new HashMap<String, String>();
		String recIds = request.getParameter("recIds");
		params.put("recIds", Utils.getSqlSplitStr(recIds));
		msg = projectService.projectDelete(params);
		printInfo(response, msg);
	}

	/** 工程停用 */
	@RequestMapping("/projectDisable")
	public void projectDisable(HttpServletRequest request,
			HttpServletResponse response) {
		HashMap<String, String> params = new HashMap<String, String>();
		String recIds = request.getParameter("recIds");
		params.put("recIds", Utils.getSqlSplitStr(recIds));
		msg = projectService.projectDisable(params);
		printInfo(response, msg);
	}

	/** 工程启用 */
	@RequestMapping("/projectEnable")
	public void projectEnable(HttpServletRequest request,
			HttpServletResponse response) {
		HashMap<String, String> params = new HashMap<String, String>();
		String recIds = request.getParameter("recIds");
		params.put("recIds", Utils.getSqlSplitStr(recIds));
		msg = projectService.projectEnable(params);
		printInfo(response, msg);
	}

	
	/** 工程附件查询 */
	@RequestMapping("/getProjectContracts")
	public void getProjectContracts(HttpServletRequest request,
			HttpServletResponse response) {
		HashMap<String, String> params = new HashMap<String, String>();
		setReqParams(request, null, params);
		List<?> projectList = ProjectMethods.getContractList(params);
		JSONObject json = new JSONObject();
		json.put("list", projectList);
		json.put("params", params);
		printInfo(response, json);
	}
	
	
	/** 工程附件删除 */
	@RequestMapping("/projectContractDel")
	public void projectContractDel(HttpServletRequest request,
			HttpServletResponse response) {
		HashMap<String, String> params = new HashMap<String, String>();
		setReqParams(request, null, params);
		int delCount = ProjectMethods.delContractById(params);
		if(delCount==0){
			msg="合同附件删除失败!";
		}else{
			msg="";
		}
		printInfo(response, msg);
	}
	
	
	/** 工程成列表（适用于下拉列表框） */
	@RequestMapping("/getProjectList")
	public void getProjectList(HttpServletRequest request,
			HttpServletResponse response) {
		HashMap<String, String> params = new HashMap<String, String>();
		setReqParams(request, null, params);
		List<?> projectList = ProjectMethods.getProjectList(params);
		JSONObject json = new JSONObject();
		json.put("list", projectList);
		json.put("params", params);
		printInfo(response, json);
	}

}
